#ifndef RSSFEED_H
#define RSSFEED_H

#include <QString>
#include <QList>
#include <QTreeWidget>
#include <QXmlStreamReader>
#include <QDomElement>
#include <QMessageBox>
#include <QDomDocument>
#include <QTextCodec>

#include "rssitem.h"
#include "functions.h"


using namespace RssGuard;


class RssFeed : QObject {
        Q_OBJECT

    private:
        QString title;
        QString link;
        QString cat;
        FeedType type;
        QByteArray encoding;
        QList<RssItem*>* msgs;
        QNetworkReply::NetworkError state;

    public:
        RssFeed(const QString &title, const QString &link, const QString &cat, FeedType type = FEED_UNKNOWN, const QString &enc = "UTF-8", const QNetworkReply::NetworkError &code = QNetworkReply::NoError);
        ~RssFeed() { delete msgs; }

        QString getLink() const {
            return link;
        }

        QString getCategory() const {
            return cat;
        }

        QString getTitle() const {
            return title;
        }

        QString getEncoding() const {
            return QString(encoding);
        }

        RssFeed *setEncoding(const QString &enc) {
            this->encoding = enc.toLocal8Bit();
            return this;
        }

        void setState(QNetworkReply::NetworkError code) {
            state = code;
        }

        QNetworkReply::NetworkError getState() const {
            return state;
        }

        QList<RssItem*> *getMsgs() {
			return msgs;
        }

        FeedType getType() const {
            return type;
        }

		RssFeed *setTitle(const QString &title) {
            this->title = title;
            return this;
        }

		RssFeed *setLink(const QString &link) {
            this->link = link;
            return this;
        }

		RssFeed *setCategory(const QString &cat) {
            this->cat = cat;
            return this;
        }

		RssFeed *setType(const FeedType &type) {
            this->type = type;
            return this;
        }

        void hideMessages(bool onlySeen = false);
        void trashAllMessages();
        void unhideMessages();

        void addMessage(const QString &title, const QString &link, const QString &description, const QString &date, bool seen = false, bool hidden = false);
		void deleteMessage(const QString &linkOfMessage);
        void markAllVisibleMessages(bool readUnread = true);
        bool hasUnseenMessages();

		int countOfUnseenMessages() {
			int count = 0;
			foreach (RssItem *msg, *getMsgs())
				if (!msg->isSeen() && !msg->isHidden())
					count++;
			return count;
		}

		int countOfVisibleMessages() {
			int count = 0;
			foreach (RssItem *msg, *getMsgs())
				if (!msg->isHidden())
					count++;
			return count;
		}

		void updateRepresentation(QTreeWidget *widget, bool append = false);

        RssItem *getItemByLink(const QString &link);
		RssItem *getItemByName(const QString &name);

        bool isntInRssFeed(QDomElement node);
        bool isntInAtomFeed(QDomElement node);

		void parseAsRss(const QString &array);
		void parseAsAtom(const QString &array);
		static FeedType guessFeed(const QString &link, QString *out);

        QNetworkReply::NetworkError parseFeed(int timeout);

};


#endif // RSSFEED_H
